from baseview import BaseView
from ..devicetree import DeviceTree
from ..bases import widgets
from .. import editors

from ..bases.propertylist import PropertyListModel

class UniversesView(BaseView):
    topwidget_name = 'Universes'
    topwidget_cls = widgets.Notebook
    def __init__(self, **kwargs):
        self.topwidget = widgets.VBox()
        super(UniversesView, self).__init__(**kwargs)
        self.DeviceSystem = self.MainController.DeviceSystem
        d = {'Universes':self.DeviceSystem.universes, 
             'Input_Universes':self.DeviceSystem.input_universes}
        for name, childgroup in d.iteritems():
            e = self.add_editor(UniverseList, name, universes=childgroup)
            self.topcontainer.add_page(label=' '.join(name.split('_')), widget=e.topwidget)
        
        self.topwidget.show_all()
        
    def unlink(self):
        super(UniversesView, self).unlink()
        
    
        
class UniverseList(editors.EditorBase):
    def __init__(self, **kwargs):
        kwargs['topwidget'] = widgets.VPane()
        super(UniverseList, self).__init__(**kwargs)
    def init_editor(self, **kwargs):
        self.DeviceSystem = self.MainController.DeviceSystem
        self.universes = kwargs.get('universes')
        if 'input' in self.universes.name.lower():
            self.universe_type = 'input'
        else:
            self.universe_type = 'output'
        self.universe_editor = None
        
        vbox = widgets.VBox()
        self.universe_list = None
        self.universe_list_widget = widgets.TreeView(name='Universes')
        self.universe_list_widget.bind(selected=self.on_universe_list_widget_selection)
        self.build_universe_list()
        vbox.pack_start(self.universe_list_widget.topwidget, expand=True)
        
        hbox = widgets.HBox()
        d = {'btnNewUniverse':'New Universe', 'btnDelUniverse':'Delete'}
        for k, v in d.iteritems():
            btn = widgets.Button(label=v)
            btn.connect('clicked', self.on_universe_list_btns_clicked, k)
            hbox.pack_start(btn)
        vbox.pack_start(hbox)
        self.topwidget.pack_start(vbox)
        
        self.universes.bind(child_update=self.on_universes_child_update)
        
    def unlink(self):
        self.universes.unbind(self)
        if self.universe_list:
            self.universe_list.unlink()
        super(UniverseList, self).unlink()
        
    def build_universe_list(self):
        if not len(self.universes):
            return
        if self.universe_list is not None:
            return
        cg = self.universes
        self.universe_list = PropertyListModel(child_group=self.universes)
        self.universe_list.add_treeview(treeview=self.universe_list_widget, hidden_props=['id'])
    
    def set_current_universe(self, obj):
        self.remove_child_editor('universe_editor')
        if not obj:
            return
        self.universe_editor = self.make_child_editor(editors.UniverseEditor, 
                                                      'universe_editor', 
                                                      universe=obj)
        self.topwidget.pack_start(self.universe_editor.topwidget, expand=True)
        self.topwidget.normalized_pos = .4
    
    def on_universe_list_widget_selection(self, **kwargs):
        #print 'selection: ', kwargs
        key = kwargs.get('value')
        univ = self.universes.get(key)
        self.set_current_universe(univ)
        
    def on_universe_list_btns_clicked(self, btn, key):
        if key == 'btnNewUniverse':
            univ = self.DeviceSystem.add_universe(type=self.universe_type)
        elif key == 'btnDelUniverse':
            objid = self.universe_list_widget.selected
            if objid is not None:
                obj = self.universes[objid]
                self.DeviceSystem.del_universe(universe=obj)
                
    def on_universes_child_update(self, **kwargs):
        obj = kwargs.get('obj')
        mode = kwargs.get('mode')
        if mode == 'add':
            if self.universe_list is None:
                self.build_universe_list()
            self.universe_list_widget.selected = obj.id

UI_VIEW = UniversesView
